package com.gxa.modules.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.gxa.modules.dto.AdressDTO;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.dtflys.forest.annotation.Get;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;

@Component
public class BaiduMap {

        /**
         * 百度地图申请的ak
         */
        //@Value("${baidu.map.ak}")
        private String AK="ZDyFcPT2afm9LoBFQ0KaHA5GkFjQO3XT";

        public String getAddress(String ip) {
            String address = "";
            try {
                // 这里调用百度的ip定位api服务 详见 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm
                JSONObject resultJson = readJsonFromUrl("https://api.map.baidu.com/location/ip?&ak=" + AK);
                System.out.println(resultJson);
                //resultJson 是返回结果，当前只取位置信息
                address = ((JSONObject) resultJson.get("content")).getString("address_detail");
                AdressDTO adressDTO = JSON.parseObject(address, AdressDTO.class);
                address=adressDTO.getAdcode();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return address;
        }

        /**
         * 读取
         *
         * @param rd
         * @return
         * @throws IOException
         */
        private static String readAll(BufferedReader rd) throws IOException {
            StringBuilder sb = new StringBuilder();
            int cp;
            while ((cp = rd.read()) != -1) {
                sb.append((char) cp);
            }
            return sb.toString();
        }

        /**
         * 创建链接
         *
         * @param url
         * @return
         * @throws IOException
         * @throws JSONException
         */
        private static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
            InputStream is = new URL(url).openStream();
            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                JSONObject json = JSONObject.parseObject(jsonText);
                return json;
            } finally {
                is.close();
            }
        }

        public String location(JSObject address){

            return null;
        }
    }

